<!-- 提货记录页 -->
<template>
  <view class="body">
    <Search @search="getParams" />
    <scroll-view @scrolltolower="handleScroll" scroll-y="true" style="height: 80vh;">
      <view v-for="(item, index) in formList" :key="index">
        <Item :item="item" :tabIndex="tabIndex" class="item" />
      </view>
    </scroll-view>
  </view>
</template>

<script  setup lang='ts'>
import { ref } from "vue";
import { OrderListType } from '@/type/index'
import { getPickRecord } from "@/@api/sign"; // 提货记录的接口.
import Item from './item.vue' // 展示组件
import Search from './search.vue' // 查询组件
import Taro, { useReady } from "@tarojs/taro";


const tabIndex = ref(0); // 选中的tab
const loading = ref(false);
const formList = ref<OrderListType[]>([]);

const query = ref({
  pageNum: 1,
  pageSize: 15,
})

const params = ref()


useReady(() => {
  Taro.setNavigationBarTitle({
    title: '提货查询'
  })
})


/* 获取子组件的参数 */
function getParams(data, flag) {
  console.log(data, flag, '子组件的参数')
  formList.value = [];
  loading.value = false;
  params.value = data;
  initList();
}

async function initList() {
  console.log(JSON.parse(JSON.stringify(params.value)), 'sss')
  const res = await getPickRecord(query.value, params.value);
  console.log(res, 'res')
  if (res.data.code == 200) {
    formList.value = setStatusVal([...formList.value, ...res?.data.rows])
    console.log(JSON.parse(JSON.stringify(formList.value)), '过滤处理过的数据')
    loading.value = false;
    if (formList.value.length >= res.data.total) {
      loading.value = true;
    }
  }
}

function handleScroll() {
  console.log('监听滚动')
  if (!loading.value) {
    query.value.pageNum++;
    initList();
  }
}


// 添加状态标签
function setStatusVal(obj: OrderListType[]): OrderListType[] {
  let arr = obj.map((item: OrderListType) => {
    let str = statusFilter(item.wxStatus, item.status, item.isExamine, item.billNo)
    return {
      ...item,
      statusVal: str
    }
  })
  return arr
}

// 状态标签判断
function statusFilter(wxStatus: number, status: number, isExamine: number, billNo: string) {
  if (isExamine == 1 && wxStatus != 1 && billNo) {
    return "待审核";
  } else if (wxStatus == 1) {
    return "待确认"
  } else if (wxStatus == 2) {
    if ([4, 5, 6, 7].includes(status)) {
      return "待签收";
    } else if (status == 8) {
      return "已完成";
    } else {
      return "待支付";
    }
  } else if (wxStatus == 3) {
    return "未发货";
  } else if (wxStatus == 4) {
    return "运输中";
  } else if (wxStatus == 5) {
    return "待签收";
  } else if (wxStatus == 6) {
    return "已完成";
  } else {
    return "其他";
  }
}


</script>

<style lang="scss">
.body {
  width: 100vw;
  height: 100%;
  background: #f5f5f5;
}
</style>